package com.future.mayi.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.AppraisalAdapter;
import com.future.mayi.app.App;
import com.future.mayi.fragment.AppraisalFragment;
import com.future.mayi.model.CateModel;
import com.future.mayi.response.CateListResponse;
import com.future.mayi.utils.ClickUtils;
import com.future.mayi.utils.KefuUtils;
import com.future.mayi.utils.PriceUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AppraisalActivity extends BaseTitleActivity implements AdapterView.OnItemClickListener, AppraisalAdapter.AppraisalAdapterListener {


    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_money)
    TextView tvMoney;

    private List<CateModel> cateList;

    private List<String> listTitle;
    private List<AppraisalFragment> listFragment;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("鉴定");
        getRightImg().setImageResource(R.drawable.bottom_kefu);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_appraisala);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {
        if (cateList == null) {
            showLoadingDialog();
            new HttpRequest<CateListResponse>()
                    .with(this)
                    .addParam("pid", 0)
                    .addParam("token", App.getToken(mContext))
                    .setApiCode(ApiCst.getBrandOrCate)
                    .setListener(new HttpRequest.OnNetworkListener<CateListResponse>() {
                        @Override
                        public void onSuccess(CateListResponse response) {
                            cateList = response.getCateList();
                            initDates();
                        }

                        @Override
                        public void onFail(String message, CateListResponse response) {

                        }
                    })
                    .start(new CateListResponse());
            return;
        }

        if (cateList.size() == 0) return;

        listTitle = new ArrayList<>(cateList.size());
        listFragment = new ArrayList<>(cateList.size());
        for (CateModel cateModel : cateList) {
            if (cateModel == null) continue;
            if (cateModel.getList() != null) continue;
            listTitle.add(cateModel.getCate_name());
            AppraisalFragment fragment = new AppraisalFragment();
            fragment.setCateModel(cateModel, this);
            listFragment.add(fragment);
        }
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listFragment.get(position);
            }

            @Override
            public int getCount() {
                return listFragment.size();
            }
        });
        initIndicator();
        dissmissLoadingDialog();

    }

    @Override
    protected void initListener() {
        getRightImg().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KefuUtils.startActivity(mContext);
            }
        });
    }


    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        if (ClickUtils.isNoLogin(mContext, true)) return;
        List<CateModel> list = new ArrayList<>();
        double money = 0;
        for (CateModel cateModel : cateList) {
            if (cateModel != null && cateModel.getList() != null && cateModel.getList().size() > 0) {
                for (CateModel model : cateModel.getList()) {
                    if (model != null && model.getNum() != 0) {
                        money = money + PriceUtils.getMultiply(model.getNum(), model.getPrice());
                        list.add(model);
                    }
                }
            }
        }


        if (list.size() == 0) {
            toast("请选择鉴定的商品");
            return;
        }

        Intent intent = new Intent(this, NewAppraisalOrderActivity.class);
        intent.putExtra("money", money);
        intent.putExtra("list", (Serializable) list);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }


    @Override
    public void onChange() {
        if (cateList == null || cateList.size() == 0) return;
        double money = 0;
        for (CateModel cateModel : cateList) {
            if (cateModel != null && cateModel.getList() != null && cateModel.getList().size() > 0) {
                for (CateModel model : cateModel.getList()) {
                    if (model != null && model.getNum() != 0) {
                        money = money + PriceUtils.getMultiply(model.getNum(), model.getPrice());
                    }
                }
            }
        }
        tvMoney.setText(String.valueOf(PriceUtils.getPriceText(money)));
    }


    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return listTitle.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(listTitle.get(index));
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.main_text_color));

                simplePagerTitleView.setPadding(CommonUtils.dip2px(getActivity(), 22), 0,
                        CommonUtils.dip2px(getActivity(), 22), 0);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.main_color));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.main_color));
                indicator.setRoundRadius(CommonUtils.dip2px(getActivity(), 1));
                indicator.setLineWidth(CommonUtils.dip2px(getActivity(), 20));
                indicator.setLineHeight(CommonUtils.dip2px(getActivity(), 2));
                indicator.setMode(2);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }
}
